package fruitsManage;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 销售记录
 * 
 * 原则上销售记录不允许修改和删除
 * 
 * @author 伟❤志
 *
 */
public class SalesModel {

	private String fruitName;// 水果名
	private double price;// 售价
	private double salesNum;// 销售量
	private Date time;// 销售时间
	private double discount;// 销售折扣

	public String getFruitName() {
		return fruitName;
	}

	public void setFruitName(String fruitName) {
		this.fruitName = fruitName;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public double getSalesNum() {
		return salesNum;
	}

	public void setSalesNum(double salesNum) {
		this.salesNum = salesNum;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public double getDiscount() {
		return discount;
	}

	public void setDiscount(double discount) {
		this.discount = discount;
	}

	/**
	 * 查询全部销售记录
	 */
	public void selectAll() {
		for (SalesModel salesModel : Sales.sales) {
			salesInfo(salesModel);
		}
	}

	/**
	 * 根据水果名查询销售记录
	 * 
	 * @param name
	 */
	public void selectByName(String name) {
		for (SalesModel salesModel : Sales.sales) {
			if (name.equals(salesModel.getFruitName())) {
				salesInfo(salesModel);
			}
		}
	}

	/**
	 * 每条销售记录
	 * 
	 * @param salesModel
	 */
	public void salesInfo(SalesModel salesModel) {
		System.out.println("==================");
		System.out.println("水果名：" + salesModel.getFruitName());
		System.out.println("售价：" + salesModel.getPrice() + " 元/斤");
		System.out.println("销售量：" + salesModel.getSalesNum() + " 斤");
		double money = salesModel.getPrice() * salesModel.getSalesNum()*salesModel.getDiscount();
		System.out.println("总价格：" + money);

		// 将时间格式化
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
		System.out.println("销售时间：" + sdf.format(salesModel.getTime()));

		System.out.println("折扣：" + salesModel.getDiscount());
		System.out.println("==================");

	}

	/**
	 * 添加销售订单
	 * 
	 * @param name
	 * @param num
	 */
	public void addSale(String name, double oPrice, double num, int sex) {
		SalesModel salesModel = new SalesModel();

		salesModel.setDiscount(1);// 正常情况下不打折
		if (sex == 1) {
			salesModel.setDiscount(0.8);// 女性打八折
		}

		salesModel.setFruitName(name);
		salesModel.setSalesNum(num);

		Date date = new Date();
		salesModel.setTime(date);

		Calendar time = Calendar.getInstance();
		int hour = time.get(Calendar.HOUR_OF_DAY);// 获取小时

		if (hour >= 17) {
			salesModel.setDiscount(0.5);// 每天下午五点以后打五折
		}

		double price = oPrice * salesModel.getDiscount();// 折后价格
		Fruits.salesTotal += price * num;
		salesModel.setPrice(oPrice);

		Sales.sales.add(salesModel);

		selectByName(name);
	}
}
